<%var styleBlock={%>
<style>
    .dialogStyle {
        width: 400px;
        height: 200px;
        padding: 10px 20px
    }
</style>
<%};%>

<% var mainBlock={%>
<div id="toolbar">
    <%for (b in buttons) {%>
    <%if(b.isShow){%>
    <a href="#" class="easyui-linkbutton" iconCls="${b.icon}" plain="true" onclick="_${b.description}()">${b.name}</a>
    <%}%>
    <%}%>

</div>
<!--列表-->
<table id="dg" title="" style="width:100%;height:90%"
       data-options="
                url: '${ctx}/admin/auth/menu/query.do',
                method: 'get',
                lines: true,
                idField: 'id',
                treeField: 'name',
                singleSelect:true,
                pagination:true,
            ">
</table>

<!--新增菜单对话框-->
<div id="create-dlg" class="easyui-dialog dialogStyle"
     data-options="iconCls:'icon-save',maximizable: true,resizable:true,modal:true" style="width: 400px"
     closed="true" buttons="#create-dlg-buttons">
    <div class="ftitle"></div>
    <form id="create-fm" method="post">
        <input type="hidden" name="id" value=""/>
        <input type="hidden" name="parentId" id="parentId" value=""/>
        <input type="hidden" name="isMenu" id="isMenu" value=""/>

        <div class="fitem padding5px">
            <label class="formLabel">菜单名:</label>
            <input name="name" class="easyui-textbox formInputText" required="true">
        </div>
        <div class="fitem padding5px">
            <label class="formLabel">url:</label>
            <input class="easyui-textbox formInputText" name="url"/>
        </div>
        <div class="fitem padding5px">
            <label class="formLabel">状态：</label>
            <select class="easyui-combobox" name="enable" required="true">
                <option value="1" selected>有效</option>
                <option value="0">无效</option>
            </select>
        </div>
    </form>
</div>
<div id="create-dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6 width90px" iconCls="icon-ok "
       onclick="_createSave()">确定</a>
    <a href="javascript:void(0)" class="easyui-linkbutton width90px" iconCls="icon-cancel"
       onclick="javascript:$('#create-dlg').dialog('close')">取消</a>
</div>


<!--新增按钮对话框-->
<div id="create-btn-dlg" class="easyui-dialog dialogStyle"
     data-options="iconCls:'icon-save',maximizable: true,resizable:true,modal:true" style="width: 400px"
     closed="true" buttons="#create-btn-dlg-buttons">
    <div class="ftitle"></div>
    <form id="create-btn-fm" method="post">
        <input type="hidden" name="id" value=""/>
        <input type="hidden" name="isMenu" id="_isMenu" value=""/>
        <input type="hidden" name="parentId" id="btn-parentId" value=""/>

        <div class="fitem padding5px">
            <label class="formLabel">按钮名:</label>
            <input name="name" class="easyui-textbox formInputText" required="true">
        </div>
        <div class="fitem padding5px">
            <label class="formLabel">图标:</label>
            <input class="easyui-textbox formInputText" name="icon" required="true"/>
        </div>
        <div class="fitem padding5px">
            <label class="formLabel">描述:</label>
            <input class="easyui-textbox formInputText" name="description" required="true"/>
        </div>
        <div class="fitem padding5px">
            <label class="formLabel">状态：</label>
            <select class="easyui-combobox" name="enable" required="true">
                <option value="1" selected>有效</option>
                <option value="0">无效</option>
            </select>
        </div>
    </form>
</div>
<div id="create-btn-dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6 width90px" iconCls="icon-ok "
       onclick="_createBtnSave()">确定</a>
    <a href="javascript:void(0)" class="easyui-linkbutton width90px" iconCls="icon-cancel"
       onclick="javascript:$('#create-btn-dlg').dialog('close')">取消</a>
</div>
<%};%>

<% var scriptBlock={%>
<script>
    $(function () {
        $('#dg').treegrid({
            columns: [[
                {field: 'ck', checkbox: true},
                {field: 'id', title: '编号'},
                {field: 'name', title: '名字'},
                {field: 'url', title: 'url'},
                {
                    field: 'isMenu', title: '菜单or按钮',
                    formatter: function (value, row, index) {
                    if (row.isMenu)
                        return '菜单';
                    else
                        return '按钮';
                    }
                },
                {field: 'icon', title: '图标'},
                {field: 'description', title: '描述'},
                {
                    field: 'enable', title: '状态',
                    formatter: function (value, row, index) {
                        if (row.enable)
                            return '有效';
                        else
                            return '无效';
                    }
                },
            ]]
        });
    });

    //新增一级菜单
    function _createFirst() {
        $('#create-dlg').dialog('open').dialog('setTitle', '新建');
        $('#create-fm').form('clear');
        $('#isMenu').val(true);
    }

    //新增二级菜单
    function _createSecond() {
        var rows = $('#dg').treegrid('getSelections');
        if(rows.length!=1) {
            $.messager.alert('注意',"请选择父菜单！");
            return;
        }

        var row = $('#dg').treegrid('getSelected');

        if(row.parentId) {
            $.messager.alert('注意', "请选择一级菜单！");
            return;
        }


        $('#create-dlg').dialog('open').dialog('setTitle', '新建');
        $('#create-fm').form('clear');
        var row = $('#dg').treegrid('getSelected');
        $('#parentId').val(row.id)
        $('#isMenu').val(true);
    }

    //新增按钮
    function _createBtn() {
        var rows = $('#dg').treegrid('getSelections');
        if (rows.length != 1) {
            $.messager.alert('注意', "请选择父菜单！");
            return;
        }

        var row = $('#dg').treegrid('getSelected');
        if (row.isMenu == false || (row.isMenu == true && row.url == undefined)) {
            $.messager.alert('注意', "请选择二级菜单！");
            return;
        }

        $('#create-btn-dlg').dialog('open').dialog('setTitle', '新建');
        $('#create-btn-fm').form('clear');
        $('#btn-parentId').val(row.id);
        $('#_isMenu').val(false);
    }

    //新增菜单保存
    function _createSave() {
        $('#create-fm').form('submit', {
            url: '${ctx}/admin/auth/menu/editSubmit.do',
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
                var result = eval('('+result+')');
                if (1==result.code && true==result.returnObject){
                    $('#create-dlg').dialog('close');		// close the dialog
                    $('#dg').treegrid('reload');	// reload the user data
                    $.messager.show({
                        title: '成功',
                        msg: '操作成功'
                    });
                } else {
                    $.messager.show({
                        title: '错误',
                        msg: result.message
                    });
                }
            }
        });
    }

    //新增菜单保存
    function _createBtnSave() {
        $('#create-btn-fm').form('submit', {
            url: '${ctx}/admin/auth/menu/editSubmit.do',
            onSubmit: function () {
                return $(this).form('validate');
            },
            success: function (result) {
                var result = eval('(' + result + ')');
                if (1 == result.code && true == result.returnObject) {
                    $('#create-btn-dlg').dialog('close');		// close the dialog
                    $('#dg').treegrid('reload');	// reload the user data
                    $.messager.show({
                        title: '成功',
                        msg: '操作成功'
                    });
                } else {
                    $.messager.show({
                        title: '错误',
                        msg: result.message
                    });
                }
            }
        });
    }

    //编辑
    function _edit() {
        var rows = $('#dg').datagrid('getSelections');

        if(rows.length!=1) {
            $.messager.alert('注意',window.edit_msg);
            return;
        }

        var row = $('#dg').treegrid('getSelected');
        if (row){
            if (row.isMenu) {
                $('#create-dlg').dialog('open').dialog('setTitle', '编辑');
                $.post('${ctx}/admin/auth/menu/edit.do?id=' + row.id, function (data) {
                    $('#create-fm').form('load', data);
                });
            } else {
                $('#create-btn-dlg').dialog('open').dialog('setTitle', '编辑');
                $.post('${ctx}/admin/auth/menu/edit.do?id=' + row.id, function (data) {
                    $('#create-btn-fm').form('load', data);
                });
            }
        }
    }


    //删除操作
    function _delete() {
        var rows = $('#dg').datagrid('getSelections');
        var i = 0;
        var rowStr = "";
        for(i;i<rows.length;i++){
            rowStr += rows[i].id;
            if(i < rows.length-1){
                rowStr += ',';
            }else{
                break;
            }
        }
        if (null !=rowStr && rowStr.length>0) {
            $.messager.confirm('注意', '你确定要删除吗?', function (r) {
                if (r) {
                    $.post('${ctx}/admin/auth/menu/delete.do', {ids: rowStr}, function (result) {
                        if (1==result.code && true==result.returnObject) {
                            $('#dg').treegrid('reload');    // reload the user data
                        } else  {
                            $.messager.show({    // show error message
                                title: '错误',
                                msg: result.message
                            });
                        }
                    }, 'json');
                }
            });
        }
    }
</script>
<%};%>

<%
var layoutParas = {"mainBlock":mainBlock,"styleBlock":styleBlock,"scriptBlock":scriptBlock};
includeFileTemplate('/layout/admin/template.htm',layoutParas){};
%>